<template>
	<div class="m-view-eSign m-window">
		<!-- <webview :src="webviewUrl"></webview> -->
	</div>
</template>

<script>
import constant from '@/public/constant';
import apienv from '@/api/env';
import _utils from '@/public/utils'
import _Api from "@/api";
import Msg from "@/public/message";

export default {
	components: {},
	data () {
		return {
			remoteUrl: apienv[constant.appenv].esign + 'SSOSign/SignLogin',
			paramsStr: '?param=',
			webviewUrl: '',
			supplierCode: '',
			supplierName: '',
			isESign: '',
			companyCode: '',
			companyName: '',
			token: '',
			eSignAccount: '',
			linkMan: '',
			phone: ''
		}
	},
	created () {
		this.webviewUrl = this.remoteUrl + this.paramsStr + this.token;
		console.log(this.webviewUrl);
	},
	mounted () {

		if (constant.accountType == "4") {
			this.getSupplierByAct();
		}
		else {
			this.getCompanyInfo();
		}


	},
	methods: {
		//获取账号对应的供应商信息，若已注册电子签就登录进去；否则自动注册
		getSupplierByAct () {
			var obj = {
				"mainAccount": constant.account
			}
			var that = this;
			_Api.getSupplierByAct(obj).then(function (response) {
				var data = response.data;

				if (data.code == "0") {
					that.showMsg(data.Error);
				} else if (data.code == "1") {

					if (data.rows.length > 0) {
						//验证是否管理员操作
						if (data.rows[0].mainAccount != constant.account) {
							that.showMsg("请使用管理员账号操作!");
						}
						else {
							that.supplierCode = data.rows[0].supplierCode;
							that.supplierName = data.rows[0].supplierName;
							that.isESign = data.rows[0].isESign;
							that.linkMan = data.rows[0].linkMan;
							that.phone = data.rows[0].linkNumber;

							//that.eSignAccount = constant.signPrefix + constant.account; 

							if (that.isESign == 0) {
								that.eSignAccount = constant.signPrefix + constant.account;
								that.register(that.supplierCode, that.supplierName, that.linkMan, that.phone, that.eSignAccount);  //未注册则注册用户   
							}
							else {
								that.eSignAccount = data.rows[0].eSignAccount;
								that.getToken(that.eSignAccount);   //注册了则单点进去   
							}

						}
					} else {
						that.showMsg("未查询到与当前账户对应的公司信息！");
					}
				}
			}).catch((e) => {
				console.log(e);
				that.showMsg("");
			})
		},

		//获取账号对应的公司信息,判断有没有开通电子签，没有就注册账号；否则登录进去
		getCompanyInfo () {

			this.companyCode = constant.bukrs;
			this.companyName = constant.butxt;
			this.linkMan = constant.employeeName;
			this.phone = constant.phone;
			var obj = {
				"state": 0,
				"name": this.companyCode,
				"current": 1,
				"size": 1
			};
			var that = this;
			_Api.getCompanyInfo(obj).then(function (response) {
				var data = response.data;
				//成功
				if (data.code == "1") {
					if (data.total > 0) {
						if (data.rows) {
							that.isESign = data.rows[0].isESign;
							//that.eSignAccount = constant.signPrefix + that.companyCode;								
							//判断是否未注册
							if (that.isESign == 0) {
								that.eSignAccount = constant.signPrefix + that.companyCode;
								that.register(that.companyCode, that.companyName, that.linkMan, that.phone, that.eSignAccount);
							}
							else {
								that.eSignAccount = data.rows[0].eSignAccount; //jjc20181104
								that.getToken(that.eSignAccount);
							}
						}
					}
					else {
						that.showMsg("公司:" + that.companyCode + " 不存在!");
					}
				} else {
					that.showMsg(data.info);
				}
			}).catch((e) => {
				console.log(e);
				that.showMsg("");
			})
		},

		//若是未注册，则注册电子签账户
		register (code, name, linkman, phone, account) {
			//提示
			if (linkman == '' || linkman == null) {
				this.$alert("当前账号未维护签章人姓名，请到【系统管理-->供应商资料完善】进行对应供应商信息的维护！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				this.removeTab(this.editableTabsValue);
				return;
			} else if (phone == '' || phone == null) {
				this.$alert("当前账号未维护签章人手机，请到【系统管理-->供应商资料完善】进行对应供应商信息的维护！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				this.removeTab(this.editableTabsValue);
				return;
			} else {
				this.$confirm("你正在为企业：" + name + "   注册电子签账号，联系人：" + linkman + "   联系人手机：" + phone + "。 如信息无误请点击【确定】，否则点击【取消】后完善信息！", "消息提示", {
					title: '提示',
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					distinguishCancelAndClose: true
				}).then(() => {

					var obj = {};
					if (constant.accountType == "4") {
						obj = {
							"name": code,
							"flag": "1",
							"account": account
						}
					}
					else {
						obj = {
							"name": code,
							"flag": "2",
							"companyName": name,
							"phone": phone,
							"account": account
						}

					}
					var that = this;
					_Api.registerVisa(obj).then(function (response) {
						var data = response.data;
						//成功是回写标识	
                        let code = ''
                        Object.keys(data).forEach(item => {
                            if(item.toLowerCase()=="code"){
                                code = item
                            }
                        });
						if (data[code] == "0" || (data[code] == "1" && data.Error.includes("被注册") == true)) {
							if (data[code] == "0") {
                                if(data.Data){
                                    that.eSignAccount = data.Data.account;//获取返回的账号  jjc20181104
                                    that.getToken(that.eSignAccount);
                                } else if(data.info){
                                    Msg.showPopMsg(data.info)
                                    that.removeTab()
                                }
							} else {
								that.getToken(that.eSignAccount);
								//   that.updateSignFlag(code, that.eSignAccount);
								// that.getEsignAccountByName(name);  //若已经注册过电子签账号，则同步到srm中
							}
						} else {
							that.showMsg(data.Error);
						}

					}).catch((e) => {
						console.log(e);
						that.showMsg("");
					})
				}).catch(() => {
					this.removeTab();
				})
			}
		},

		//获取电子签账号信息
		getEsignAccountByName (name) {

		},

		//回写注册状态
		updateSignFlag (code, eSignAccount) {
			var obj = {};
			if (constant.accountType == "4") {
				obj = {
					"supplierCode": code,
					"flag": "1",
					"eSignAccount": eSignAccount
				}
			}
			else {
				obj = {
					"companyCode": code,
					"flag": "2",
					"eSignAccount": eSignAccount
				}
			}
			var that = this;
			_Api.updateSignFlag(obj).then(function (response) {

			}).catch((e) => {
				console.log(e);
				that.showMsg("");
			})
		},

		//获取电子签系统登录token
		getToken (eSignAccount) {

			var obj = {
				"name": eSignAccount
			}
            var that = this;
			_Api.getESignToken(obj).then(function (response) {
				var data = response.data;
				if (data.Code == "1") {
					that.showMsg(data.Error);
				} else if (data.Code == "0") {
					that.token = data.Data;
					that.SSOLogin();
				}
			}).catch((e) => {
				console.log(e);
				that.showMsg("");
			})
		},

		//弹出电子签系统，并单点登录进去
		SSOLogin () {
			this.webviewUrl = this.remoteUrl + this.paramsStr + this.token;
			if (constant.userAgent == 'web') {
				_utils.openNewWindow(this.webviewUrl)
			} else {
				shell.openExternal(this.webviewUrl)
			}
			this.removeTab()
		},

		//弹出消息
		showMsg (msg) {
			this.$alert("操作失败！" + msg, {
				showClose: false,
				confirmButton: "确定",
				customClass: "alertFailure"
			});
			this.removeTab();
		},

	},
	props: {
		removeTab: {
			type: Function
		}
	}
};
</script>
